package com.bawei.lib_common.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.CountDownTimer
import android.util.AttributeSet
import android.view.View
import kotlin.math.min

class CountdownCircleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    companion object {
        private const val DEFAULT_SIZE_DP = 36
        private const val DEFAULT_COUNTDOWN_START = 5
    }

    private val circlePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        style = Paint.Style.STROKE
        strokeWidth = 5f
    }

    private val textPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        textSize = convertDpToPixel(13f, context).toFloat()
        textAlign = Paint.Align.CENTER
    }

    var currentNumber: Int = DEFAULT_COUNTDOWN_START
    private var countDownTimer: CountDownTimer? = null
    private val viewSize: Int = convertDpToPixel(DEFAULT_SIZE_DP.toFloat(), context)
    private val strokeWidth = 5f

    init {
        startCountdown()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        val size = min(min(width, height), viewSize)
        setMeasuredDimension(size, size)
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val center = width / 2f
        val radius = (width - strokeWidth) / 2f
        // 绘制白色圆形
        canvas.drawCircle(center, center, radius, circlePaint)

        // 绘制居中数字
        val text = "${currentNumber}s"
        val textBounds = Rect()
        textPaint.getTextBounds(text, 0, text.length, textBounds)
        val y = center - (textBounds.top + textBounds.bottom) / 2f
        canvas.drawText(text, center, y, textPaint)
    }

    interface OnCountdownFinishListener {
        fun onCountdownFinished()
    }

    private var listener: OnCountdownFinishListener? = null

    fun setOnCountdownFinishListener(listener: OnCountdownFinishListener?) {
        this.listener = listener
    }

    fun cancel(){
        countDownTimer?.cancel()
//        currentNumber = 0
    }
    fun startCountdown() {
        cancel()

        countDownTimer = object : CountDownTimer(
            DEFAULT_COUNTDOWN_START * 1000L,
            1000L
        ) {
            override fun onTick(millisUntilFinished: Long) {
                currentNumber = (millisUntilFinished / 1000).toInt() + 1
                invalidate()
            }

            override fun onFinish() {
                currentNumber = 0
                invalidate()
                // 倒计时结束后的处理
                listener?.onCountdownFinished()
            }
        }.start()
    }

    private fun convertDpToPixel(dp: Float, context: Context): Int {
        return (dp * context.resources.displayMetrics.density).toInt()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        countDownTimer?.cancel()
    }
}